package com.jingan.nursinghome.regulatory.dialog.micrometertable;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.model.Topicinfo;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.RectificationView;
import com.jingan.nursinghome.regulatory.view.SpannerView;
import com.jingan.nursinghome.regulatory.view.iView;


import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 历史整改
 * Created by 彭治铭 on 2017/11/6.
 */

public class HostoryDialog extends BaseDialog {

    private static HostoryDialog hostoryDialog;

    public static HostoryDialog getInstance(BaseActivity baseActivity, List<Topicinfo> Topics) {
        myTopics = Topics;
//        if (isfinish(baseActivity, hostoryDialog)) {
//            hostoryDialog = new HostoryDialog(baseActivity);
//        }
        hostoryDialog = new HostoryDialog(baseActivity);
        //Log.e("test","整改数量:\t"+Topics.size());
        return hostoryDialog;
    }

    public HostoryDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_history, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.crown_window_right);
        initUI();
        //adapterUI();
    }

    MyActionBar myActionBar;

    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;

    public void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "历史整改");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity) {
            @Override
            public boolean canScrollVertically() {
                //return canScrollVertically;
                return true;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        //recyclerView.addItemDecoration(new MySpacesItemDecoration(baseActivity, 0));
    }

    public void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_micrometertableos_constraintLayout));
    }

    private static List<Topicinfo> myTopics;

    @Override
    protected void listener() {
        //recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity));
        recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity,myTopics));
    }

    @Override
    protected void recycleView() {

    }

    @Override
    protected boolean isRecycel() {
        return false;
    }

    //适配器开始

//    public class MicrometerTableAdapter extends RecyclerView.Adapter<HostoryDialog.MicrometerTableAdapter.MyViewHolder> {
//        BaseActivity baseActivity;
//
//        public MicrometerTableAdapter(BaseActivity baseActivity) {
//            this.baseActivity = baseActivity;
//        }
//
//        //ViewHolder和View绑定在一起，构造函数一般都是初始化控件
//        public class MyViewHolder extends RecyclerView.ViewHolder {
//            public TextView txt_title;
//            iView txt_question;
//            com.jingan.nursinghome.regulatory.view.triangleView triangleView;
//            SpannerView spanerScore;
//            RectificationView rectifi;
//            View ling;
//
//            public MyViewHolder(View itemView) {
//                super(itemView);
//                txt_title = (TextView) itemView.findViewById(R.id.txt_title);
//                txt_question = (iView) itemView.findViewById(R.id.txt_question);
//                triangleView = (com.jingan.nursinghome.regulatory.view.triangleView) itemView.findViewById(R.id.triangleView);
//                ProportionUtils.getInstance().adapterTextView(txt_title);
//                spanerScore = (SpannerView) itemView.findViewById(R.id.spanerScore);
//                ProportionUtils.getInstance().adapterView(spanerScore);
//                rectifi = (RectificationView) itemView.findViewById(R.id.rectifi);
//                ling = itemView.findViewById(R.id.ling);
//                ProportionUtils.getInstance().adapterView(ling);
//            }
//        }
//
//
//        @Override
//        public HostoryDialog.MicrometerTableAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
//            return new HostoryDialog.MicrometerTableAdapter.MyViewHolder(view);
//        }
//
//        String cScore;
//
//        @Override
//        public void onBindViewHolder(final HostoryDialog.MicrometerTableAdapter.MyViewHolder holder, final int position) {
//            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(holder.txt_question.getLayoutParams());
//            layoutParams.leftToLeft = holder.itemView.findViewById(R.id.left).getId();
//            layoutParams.topToTop = holder.itemView.findViewById(R.id.top3).getId();
//            holder.txt_title.setText(null);
//            holder.triangleView.setInfo(null);
//            holder.ling.setVisibility(View.VISIBLE);
//            holder.txt_question.setLayoutParams(layoutParams);
//            String i = (position + 1) + "";
//            if (i.length() == 1) {
//                i = "0" + i;
//            }
//            holder.txt_question.setInfo(i, myTopics.get(position).getTtitle());//题目
//            //holder.spanerScore.setInfo("  "+myTopics.get(position).getMaxScore() + "分");//分数
//            cScore = myTopics.get(position).getScoring();
//            if (cScore == null || cScore.trim().equals("")) {
//                cScore = myTopics.get(position).getTopic_score() + "";
//                holder.spanerScore.setIsscore(false);
//            } else {
//                holder.spanerScore.setIsscore(true);
//            }
//            holder.spanerScore.setInfoAndColor("  " + cScore + "分", Color.parseColor("#444444"));
//            holder.spanerScore.setTextSize(ProportionUtils.getInstance().adapterInt(32));
//            holder.spanerScore.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (myTopics == null || myTopics.get(position) == null ||
//                            myTopics.get(position).getTopic_score() == null) {
//                        return;
//                    }
//                    ScoreDialog.getInstance(baseActivity, Integer.valueOf(cScore), Integer.valueOf(myTopics.get(position).getTopic_score()), new BaseListenner<Integer>() {
//                        @Override
//                        public void onResult(Integer result) {
//                            holder.spanerScore.setIsscore(true);
//                            holder.spanerScore.setInfoAndColor("  " + result + "分", Color.parseColor("#444444"));
//                            myTopics.get(position).setScoring(result + "");
//
//                            String c = myTopics.get(position).getScoring();
//                            String m = myTopics.get(position).getTopic_score() + "";
//                            //Log.e("test","当前分数:\t"+c+"\t最大分数:\t"+m);
//                            if ((c == null || c.trim().equals("")) || !c.trim().equals(m.trim())) {
//                                //非满分状态。弹出是否整改的询问窗口。
//                                AlertDialog.getInstance(baseActivity, false, true).setMession("是否要整改？").setPositive("确认", new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        AlertDialog.dismisss();
//                                        ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(myTopics.get(position).getZhengai()), new BaseListenner<Integer>() {
//                                            @Override
//                                            public void onResult(Integer result) {
//                                                myTopics.get(position).setZhengai(result + "");
//                                                if (myTopics.get(position).getZhengai().equals("0")) {
//                                                    holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
//                                                }
//                                                if (myTopics.get(position).getZhengai().equals("1")) {
//                                                    holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
//                                                }
//                                                if (myTopics.get(position).getZhengai().equals("2")) {
//                                                    holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
//                                                }
//                                            }
//                                        });
//                                    }
//                                });
//                            }
//
//                        }
//                    });
//                }
//            });
//
//            if (myTopics.get(position).getZhengai().equals("0")) {
//                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
//            }
//            if (myTopics.get(position).getZhengai().equals("1")) {
//                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
//            }
//            if (myTopics.get(position).getZhengai().equals("2")) {
//                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
//            }
//            holder.rectifi.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(myTopics.get(position).getZhengai()), new BaseListenner<Integer>() {
//                        @Override
//                        public void onResult(Integer result) {
//                            myTopics.get(position).setZhengai(result + "");
//                            if (myTopics.get(position).getZhengai().equals("0")) {
//                                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
//                            }
//                            if (myTopics.get(position).getZhengai().equals("1")) {
//                                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
//                            }
//                            if (myTopics.get(position).getZhengai().equals("2")) {
//                                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
//                            }
//                        }
//                    });
//                }
//            });
//
//            //高度調整
//            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().height = ProportionUtils.getInstance().adapterInt(330) + (holder.txt_question.LineCount - 1) * ProportionUtils.getInstance().adapterInt(40);
//            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().width = (int) ProportionUtils.getInstance().realWidthPixels;
//            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).requestLayout();
//
//            //Log.e("test", "题号:\t" + i + "\t行数:\t" + holder.txt_question.LineCount);
//        }
//
//        @Override
//        public int getItemCount() {
//            return myTopics.size();
//        }
//    }

    //适配器结束

}
